// *** Server Object *** //
// Controls the communication to the server
//
// @project: GoldenShield
// @author: Kasper B. Jensen
// @version: 1.0

var server = {
    file: 'index.php', //Server communication file
    
    //Loader et nyt map fra serveren
    loadMap: function(map) {
        scene.loading(map);
        $.ajax({
            url: this.file,
            type: 'get',
            data: 'r=xmap&f=loadMap&p=' + map,
            cache: false,
            async: false,
            success: function(data) {
                var load = function() {
                    $('#mapContent').html(data);
                    scene.removeLoading();
                }
                setTimeout(load, 1000);
            }
        })
    },
    
    //Makes a call with the given parameters to XMAP
    callXmapAPI: function(func, callback, parm, dataType) {
      if(!func || !callback) {
          return false;
      }
      
      var data = "r=xmap&f=" + func + "&p=" + parm
      
      $.ajax({
          url: this.file,
          type: 'get',
          data: data,
          dataType: dataType,
          success: callback
      });  
    },
    
    //Loads a script from server
    loadScript: function(script, callback) {
      $.getScript(script, callback);
    },
    
    //Loads configutaion from server
    getConfig: function(callback) {
      $.getJSON(this.file, 'r=javaConf', callback);  
    },
    
    //Loads content from the given page into the given div
    loadPage: function(page, div) {
            $.ajax({
            url: this.file,
            type: 'get',
            data: 'r=getPage&value=' + page,
            cache: false,
            success: function(data) {
                $(div).html(data);
            }
        })
    },
    
    //Post content to server from game
    postFormToPage: function(page, div, sucessText) {
            var postData = $('input').serialize();
            $.ajax({
            url: this.file,
            type: 'post',
            data: 'r=getPage&value=' + page + '&' + postData,
            cache: false,
            success: function(data) {
                $(div).html(data);
            }
        })
    }
}